package com.xjgzinfo.usdp.service;

import com.xjgzinfo.usdp.domain.BladeVisualRecord;
import com.xjgzinfo.usdp.domain.BladeVisualRecord_;
import com.xjgzinfo.usdp.jpa.service.QueryService;
import com.xjgzinfo.usdp.repository.BladeVisualRecordRepository;
import com.xjgzinfo.usdp.service.dto.BladeVisualRecordCriteria;
import com.xjgzinfo.usdp.service.dto.BladeVisualRecordDTO;
import com.xjgzinfo.usdp.service.mapper.BladeVisualRecordMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service for executing complex queries for {@link BladeVisualRecord} entities in the database.
 * The main input is a {@link BladeVisualRecordCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link BladeVisualRecordDTO} or a {@link Page} of {@link BladeVisualRecordDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class BladeVisualRecordQueryService extends QueryService<BladeVisualRecord> {

    private final Logger log = LoggerFactory.getLogger(BladeVisualRecordQueryService.class);

    private final BladeVisualRecordRepository bladeVisualRecordRepository;

    private final BladeVisualRecordMapper bladeVisualRecordMapper;

    public BladeVisualRecordQueryService(BladeVisualRecordRepository bladeVisualRecordRepository, BladeVisualRecordMapper bladeVisualRecordMapper) {
        this.bladeVisualRecordRepository = bladeVisualRecordRepository;
        this.bladeVisualRecordMapper = bladeVisualRecordMapper;
    }

    /**
     * Return a {@link List} of {@link BladeVisualRecordDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<BladeVisualRecordDTO> findByCriteria(BladeVisualRecordCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<BladeVisualRecord> specification = createSpecification(criteria);
        return bladeVisualRecordMapper.toDto(bladeVisualRecordRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link BladeVisualRecordDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page     The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<BladeVisualRecordDTO> findByCriteria(BladeVisualRecordCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<BladeVisualRecord> specification = createSpecification(criteria);
        return bladeVisualRecordRepository.findAll(specification, page)
                .map(bladeVisualRecordMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(BladeVisualRecordCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<BladeVisualRecord> specification = createSpecification(criteria);
        return bladeVisualRecordRepository.count(specification);
    }

    /**
     * Function to convert {@link BladeVisualRecordCriteria} to a {@link Specification}
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<BladeVisualRecord> createSpecification(BladeVisualRecordCriteria criteria) {
        Specification<BladeVisualRecord> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getId(), BladeVisualRecord_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), BladeVisualRecord_.name));
            }
            if (criteria.getUrl() != null) {
                specification = specification.and(buildStringSpecification(criteria.getUrl(), BladeVisualRecord_.url));
            }
            if (criteria.getDataType() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDataType(), BladeVisualRecord_.dataType));
            }
            if (criteria.getDataMethod() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDataMethod(), BladeVisualRecord_.dataMethod));
            }
            if (criteria.getDataHeader() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDataHeader(), BladeVisualRecord_.dataHeader));
            }
            if (criteria.getDataQuery() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDataQuery(), BladeVisualRecord_.dataQuery));
            }
            if (criteria.getDataQueryType() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDataQueryType(), BladeVisualRecord_.dataQueryType));
            }
            if (criteria.getWsUrl() != null) {
                specification = specification.and(buildStringSpecification(criteria.getWsUrl(), BladeVisualRecord_.wsUrl));
            }
            if (criteria.getIsDeleted() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsDeleted(), BladeVisualRecord_.isDeleted));
            }
        }
        return specification;
    }
}